var ldap = require('ldapjs');
 var obj = [
  {
    dn: 'dc=structure-net,dc=de',
    attributes: {
      objectclass: ['top', 'organization'],
      o: ['dc=structure-net, dc=de']
    }, 
    children:[
    {
    dn: 'ou=example2,dc=structure-net,dc=de',
    attributes: {
      objectclass: ['top','organizationalunit'],
      ou: ['example2']
    },
    children: [
  	{
    	dn: 'ou=example3,ou=example2,dc=structure-net,dc=de',
    	attributes: {
    	  objectclass: ['top','organizationalunit'],
      	  ou: ['example3']
    	}, children:[]
	}
	]
  }]
}
];
var handlers = {};
var searchClient = require('./client.js');
console.log(searchClient);
searchClient.client.finishedCallback = (function(entries) {
	obj = { dn: 'dc=example,dc=com',
            objectclass: ['top', 'organization'],
            o: ['dc=structure-net, dc=de']
    	};
	obj['member'] = [];
	entries.forEach(function(entry) {
		obj['member'].push(entry.dn);
        	walkThrough(entry, 'member');
	});
	console.log(obj);
	var server = ldap.createServer();
	console.log('starting server');
	server.listen(1389, function() {
        walkThrough(obj, 'member');
		server.search("dc=example,dc=com", function(req, res, next) {
		console.log(req.dn.toString().replace(/,\s+/g, ',').toLowerCase());
        var handler=handlers[req.dn.toString().replace(/,\s+/g, ',').toLowerCase()];
		console.log(handler);
		console.log(handlers);
		console.log('base object: ' + req.dn.toString());
         	console.log('scope: ' + req.scope);
          	console.log('filter: ' + req.filter.toString());

        	handler(req,res,next);
		res.end();
	});
});
});

searchClient.client.start();


function walkThrough(obj, childAttributeName) {
	console.log(obj);
	console.log(' added handler for' + obj.dn);
	var dn = obj.dn.replace(/,\s+/g, ',').toLowerCase();
 	handlers[dn] = (function(req, res, next) {

	  console.log('base object: ' + req.dn.toString());
  	  console.log('scope: ' + req.scope);
  	  console.log('filter: ' + req.filter.toString());
          console.log('searched ' + dn);
          var objectToSend = {
    		dn: obj.dn,
    		attributes: 
		obj,
    	  };

	console.log('====');
	console.log(objectToSend);
	console.log('====');
	  if(req.scope=="base") {
              	res.send(objectToSend);
	  } else {
             	res.send(objectToSend);
		console.log(obj);
		if(typeof(obj[childAttributeName])==="undefined") { } 
		else if(typeof(obj[childAttributeName])==="string") {
		return;
			obj2 = obj[childAttributeName];
                        console.log(obj2);
                        var dn = obj2.replace(/,\s+/g, ',').toLowerCase();
                        var handler = handlers[obj2.replace(/,\s+/g, ',').toLowerCase()];
                        handler(req, res,next);
                        console.log(handler);
		} else {
		obj[childAttributeName].forEach(function(obj2) {
			console.log(obj2);
			var dn = obj2.replace(/,\s+/g, ',').toLowerCase();
			var handler = handlers[obj2.replace(/,\s+/g, ',').toLowerCase()];
			if(typeof(handler)==="undefined") { } else {
				handler(req, res,next);
			}
			console.log(handler);
		});
		}
	}
        });
}
